<?php

namespace app\common\business;
use app\common\model\mysql\SysPost as SysPostModel;
use app\common\model\mysql\SysUserPost as SysUserPostModel;

class SysPost
{
    public $postObj = null;
    public $postUserObj = null;
    public function __construct()
    {
        $this->postObj = new SysPostModel();
        $this->postUserObj = new SysUserPostModel();
    }
    //查询岗位列表
    public function getNormalPost($where,$field,$limit,$page){
        $postData = $this->postObj->getNormalPost($where,$field,$limit,$page);
        if(!$postData){
            return [];
        }
        $postData = $postData->toArray();
        return $postData;
    }

    //查询岗位数据filed数组
    public function getNormalPostFiled($where = [],$field = "post_name"){
        $roleData = $this->postObj->getNormalPostFiledArr($where,$field);
        if(!$roleData){
            return [];
        }
        return $roleData;
    }

    //查询岗位列表(无分页)
    public function getNormalPostNotPage($where = [],$field = "*"){
        $postData = $this->postObj->getNormalPostNotPage($where,$field);
        if(!$postData){
            return [];
        }
        $postData = $postData->toArray();
        return $postData;
    }

    //添加部门
    public function addPost($data){
        //根据$post_name去数据库查询是否存在这条记录
        $postNameResult = $this->getPostByName($data['post_name']);
        if($postNameResult){
            throw new \think\Exception("该岗位名称已经存在请重新设置");
        }
        //根据$post_code去数据库查询是否存在这条记录
        $postCodeResult = $this->getPostByCode($data['post_code']);
        if($postCodeResult){
            throw new \think\Exception("该岗位编号已经存在请重新设置");
        }
        try {
            $this->postObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->postObj->id;
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getPostById($id){
        $user = $this->postObj->getPostByPostId($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$ids数组，删除数据库记录delete
    public function DelPostIds($ids){
        $ids = explode(',', $ids);
        //根据$post_name去数据库查询是否存在这条记录
        $postResult = $this->postUserObj->passPostDelIds($ids);
        if($postResult){
            $postData = $this->postObj->getPostByPostId($postResult->post_id);
            throw new \think\Exception($postData->post_name."已分配,不能删除");
        }

        $result = $this->postObj->DelPostIds($ids);
        return $result;
    }

    //根据$name,去数据库查询是否存在这条记录
    public function getPostByName($name,$post_id = ''){
        $user = $this->postObj->getPostByPostName($name,$post_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$code,去数据库查询是否存在这条记录
    public function getPostByCode($code,$post_id = ''){
        $user = $this->postObj->getPostByPostCode($code,$post_id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }
    //查询岗位总条数
    public function getNormalPostCount($where){
        $postCount = $this->postObj->getNormalPostCount($where);
        if(!$postCount){
            return 0;
        }
        return $postCount;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        //根据$post_name去数据库查询是否存在这条记录
        $postNameResult = $this->getPostByName($data['post_name'],$id);
        if($postNameResult){
            throw new \think\Exception("该岗位名称已经存在请重新设置");
        }
        //根据$post_code去数据库查询是否存在这条记录
        $postCodeResult = $this->getPostByCode($data['post_code'],$id);
        if($postCodeResult){
            throw new \think\Exception("该岗位编号已经存在请重新设置");
        }
        $where = [
            "post_id"=>$id,
        ];
        return $this->postObj->where($where)->find()->save($data);
    }
}
